<template>
  <div class='container'>
     <el-dialog width="30%" title="新增目录" @close="closeDialog"  :visible="showDialog">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="所属学科" prop="subjectID">
          <el-select @focus="focusFn" v-model="ruleForm.subjectID" placeholder="请选择学科">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item  label="目录名称"  prop="tagName">
          <el-input v-model="ruleForm.tagName"></el-input>
        </el-form-item>
        <el-form-item size="large">
          <el-button @click="closeDialog">取消</el-button>
          <el-button type="primary" @click="confirm">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { simple } from '@/api/hmmm/subjects.js'
import { add, detail, update } from '@/api/hmmm/tags.js'
export default {
  props: {
    showDialog: {
      type: Boolean
    },
    data: {
      type: Object
    }
  },
  data () {
    return {
      ruleForm: { // 弹框表单
        subjectID: '',
        tagName: ''
      },
      rules: {
        tagName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ],
        subjectID: [
          { required: true, message: '必填项', trigger: 'change' }
        ]
      },
      // 弹框下拉数组
      options: []
    }
  },
  methods: {
    closeDialog () {
      this.$emit('close')
      this.ruleForm = { // 弹框表单
        subjectID: '',
        tagName: ''
      }
      this.$refs.ruleForm.resetFields()
    },
    // 获取焦点
    async focusFn () {
      const res = await simple()
      console.log(res)
      this.options = res.data
    },
    // 确认添加
    confirm () {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          if (this.data.id) {
            const res = await update(this.ruleForm)
            console.log(res)
          } else {
            console.log('ok')
            const res = await add(this.ruleForm)
            console.log(res)
          }
          this.$message.success(`${this.data.id ? '修改成功' : '添加成功'}`)
          this.$emit('load')
          this.closeDialog()
        }
      })
    },
    async detailDialog (data) {
      const res = await detail(data)
      console.log(res)
      this.ruleForm = res.data
    }
  }
}
</script>

<style scoped lang='less'></style>
